package com.ruoyi.print.controller;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.print.request.UpdateTemplateRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.print.domain.PrintTemplate;
import com.ruoyi.print.service.IPrintTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 打印模版Controller
 *
 * @author husky
 * @date 2023-11-04
 */
@Api(tags = "打印模版管理")
@RestController
@RequestMapping("/print/template")
public class PrintTemplateController extends BaseController {
    @Autowired
    private IPrintTemplateService printTemplateService;

    /**
     * 查询打印模版列表
     */
    @ApiOperation("查询打印模版列表")
    @PreAuthorize("@ss.hasPermi('print:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(PrintTemplate printTemplate) {
        startPage();
        List<PrintTemplate> list = printTemplateService.selectPrintTemplateList(printTemplate);
        return getDataTable(list);
    }

    /**
     * 导出打印模版列表
     */
    @ApiOperation("导出打印模版列表")
    @PreAuthorize("@ss.hasPermi('print:template:export')")
    @Log(title = "打印模版", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PrintTemplate printTemplate) {
        List<PrintTemplate> list = printTemplateService.selectPrintTemplateList(printTemplate);
        ExcelUtil<PrintTemplate> util = new ExcelUtil<PrintTemplate>(PrintTemplate.class);
        util.exportExcel(response, list, "打印模版数据");
    }

    /**
     * 获取打印模版详细信息
     */
    @ApiOperation("获取打印模版详细信息")
    @PreAuthorize("@ss.hasPermi('print:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(printTemplateService.getById(id));
    }

    /**
     * 新增打印模版
     */
    @ApiOperation("新增打印模版")
    @PreAuthorize("@ss.hasPermi('print:template:add')")
    @Log(title = "打印模版", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PrintTemplate printTemplate) {
        return toAjax(printTemplateService.save(printTemplate));
    }

    /**
     * 修改打印模版
     */
    @ApiOperation("修改打印模版")
    @PreAuthorize("@ss.hasPermi('print:template:edit')")
    @Log(title = "打印模版", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PrintTemplate printTemplate) {
        return toAjax(printTemplateService.updateById(printTemplate));
    }

    /**
     * 删除打印模版
     */
    @ApiOperation("删除打印模版")
    @PreAuthorize("@ss.hasPermi('print:template:remove')")
    @Log(title = "打印模版", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(printTemplateService.removeByIds(Arrays.asList(ids)));
    }

    //
    @ApiOperation("获取打印模版根据类型")
    @Anonymous
    @GetMapping("/list-template-by-type/{type}")
    public AjaxResult listTemplateByType(@PathVariable String type) {
        return AjaxResult.success(printTemplateService
                .list(new LambdaQueryWrapper<PrintTemplate>()
                        .eq(PrintTemplate::getType, type)));
    }

    @ApiOperation("获取打印模版根据类型")
    @Anonymous
    @GetMapping("/update-template/")
    public AjaxResult updateTemplateByType(@RequestBody UpdateTemplateRequest updateTemplateRequest) {
        boolean update = printTemplateService.update(new LambdaUpdateWrapper<PrintTemplate>()
                .set(PrintTemplate::getTemplate, updateTemplateRequest.getTemplate())
                .eq(PrintTemplate::getType, updateTemplateRequest.getType())
                .eq(PrintTemplate::getName, updateTemplateRequest.getName()));
        return AjaxResult.success(update);
    }


}
